package com.yypinger.gxonline;

import java.util.Locale;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.beans.factory.annotation.Qualifier;
import org.vaadin.appfoundation.i18n.Lang;

import com.vaadin.Application;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Component;
import com.vaadin.ui.CssLayout;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.HorizontalSplitPanel;
import com.vaadin.ui.NativeButton;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;
import com.vaadin.ui.themes.BaseTheme;
import com.yypinger.common.spring.SpringContextHolder;
import com.yypinger.common.vaadin.TableColumnDefinition;
import com.yypinger.common.vaadin.TableDefinition;
import com.yypinger.gxonline.view.basedata.SystemManagerTabSheet;
import com.yypinger.gxonline.view.clothmanager.ReceiveClothView;

/**
 * The Application's "main" class
 */
@SuppressWarnings("serial")
public class GxOnlineApplication extends Application implements IGxOnlineApplication
{
	 public static final String[] THEMES = { "reindeer", "runo" };
	 public static final String GXONLINE_THEME_NAME = "gxonline";
	 public static String currentApplicationTheme = GXONLINE_THEME_NAME + "-"  + THEMES[0];
	 
	 
	 
	 
    @Override
    public void init()
    {
    	//初始化语言
    	Lang.initialize(this);          
    	Lang.setLocale(Locale.SIMPLIFIED_CHINESE);  
    	//ViewHandler.initialize(this);
        setMainWindow(new GxOnlineWindow());
    }
    
    
    class GxOnlineWindow extends Window implements Button.ClickListener{
    	
    	private final String TITLE = Lang.getMessage("SystemTitle");
    	private final HorizontalSplitPanel mainSplit;
    	// breadcrumbs
        BreadCrumbs breadcrumbs = new BreadCrumbs();
        
        private VerticalLayout leftMainMenu = new VerticalLayout();
   	 
	   	 //收衣按钮
	   	 private Button receiveClothBtn = new Button();
	   	 //取衣按钮
	   	 private Button takeClothBtn = new Button();
	   	 //统计管理
	   	 private Button statisticBtn = new Button();
	   	 //系统管理
	   	 private Button systemBtn = new Button();
        
    	public GxOnlineWindow(){
    		// Main top/expanded-bottom layout
    		 VerticalLayout mainExpand = new VerticalLayout();
    		 setContent(mainExpand);
    		 setSizeFull();
             mainExpand.setSizeFull();
    		 setCaption(TITLE);
    		 setTheme(currentApplicationTheme);
    		 
    		 // topbar (navigation)
             HorizontalLayout nav = new HorizontalLayout();
             mainExpand.addComponent(nav);
             nav.setHeight("44px");
             nav.setWidth("100%");
             nav.setStyleName("topbar");
             nav.setSpacing(true);
             nav.setMargin(false, true, false, false);
             
             // Upper left logo
             Component logo = createLogo();
             nav.addComponent(logo);
             nav.setComponentAlignment(logo, Alignment.MIDDLE_LEFT);
             
             // Breadcrumbs
             nav.addComponent(breadcrumbs);
             nav.setExpandRatio(breadcrumbs, 1);
             nav.setComponentAlignment(breadcrumbs, Alignment.MIDDLE_LEFT);
             
             //顶部区域的快捷按钮
             HorizontalLayout quicknav = new HorizontalLayout();
             HorizontalLayout arrows = new HorizontalLayout();
             nav.addComponent(quicknav);
             nav.addComponent(arrows);
             nav.setComponentAlignment(quicknav, Alignment.MIDDLE_LEFT);
             nav.setComponentAlignment(arrows, Alignment.MIDDLE_LEFT);
             quicknav.setStyleName("segment");
             arrows.setStyleName("segment");
             
             
             // Main left/right split; hidden menu tree
             mainSplit = new HorizontalSplitPanel();
             mainSplit.setSizeFull();
             mainSplit.setStyleName("main-split");
             mainExpand.addComponent(mainSplit);
             mainExpand.setExpandRatio(mainSplit, 1);
             
             
             //主功能菜单
             VerticalLayout moduleMenuLayout = new VerticalLayout();
             receiveClothBtn.setCaption(Lang.getMessage("receiveClothBtnLabel"));
         	 takeClothBtn.setCaption(Lang.getMessage("takeClothBtnLabel"));
         	 statisticBtn.setCaption(Lang.getMessage("statisticBtnLabel"));
         	 systemBtn.setCaption(Lang.getMessage("systemBtnLabel"));
         	 
         	 systemBtn.addListener(this);
         	 statisticBtn.addListener(this);
         	 takeClothBtn.addListener(this);
         	 receiveClothBtn.addListener(this);
         	 
         	 moduleMenuLayout.addComponent(receiveClothBtn);
         	 moduleMenuLayout.setComponentAlignment(receiveClothBtn, Alignment.MIDDLE_CENTER);
         	 moduleMenuLayout.addComponent(takeClothBtn);
         	 moduleMenuLayout.setComponentAlignment(takeClothBtn, Alignment.MIDDLE_CENTER);
         	 moduleMenuLayout.addComponent(statisticBtn);
         	 moduleMenuLayout.setComponentAlignment(statisticBtn, Alignment.MIDDLE_CENTER);
         	 moduleMenuLayout.addComponent(systemBtn);
         	 moduleMenuLayout.setComponentAlignment(systemBtn, Alignment.MIDDLE_CENTER);
         	 
             mainSplit.setFirstComponent(moduleMenuLayout);
             
             
             //默认主应用界面
             ReceiveClothView receiveClothView = new ReceiveClothView(null);
             mainSplit.addComponent(receiveClothView);
             
            
             
             //显示/隐藏主菜单按钮
             Component treeSwitch = createMenuLayoutSwitch();
             quicknav.addComponent(treeSwitch);
             
             addListener(new CloseListener() {
                 public void windowClose(CloseEvent e) {
                     if (getMainWindow() != GxOnlineWindow.this) {
                    	 GxOnlineApplication.this.removeWindow(GxOnlineWindow.this);
                     }
                 }
             });
    	}
    	
    	 private Component createLogo() {
             Button logo = new NativeButton("", new Button.ClickListener() {
                 public void buttonClick(ClickEvent event) {
                     
                 }
             });
             logo.setDescription("↶ Home");
             logo.setStyleName(BaseTheme.BUTTON_LINK);
             logo.addStyleName("logo");
             return logo;
         }
    	 
    	 
    	 private Component createMenuLayoutSwitch() {
             final Button b = new NativeButton();
             b.setStyleName("tree-switch");
             b.addStyleName("down");
             b.addListener(new Button.ClickListener() {
                 public void buttonClick(ClickEvent event) {
                     if (b.getStyleName().contains("down")) {
                         b.removeStyleName("down");
                         mainSplit.setSplitPosition(0);
                         mainSplit.setLocked(true);
                     } else {
                         b.addStyleName("down");
                         mainSplit
                                 .setSplitPosition(150, HorizontalSplitPanel.UNITS_PIXELS);
                         mainSplit.setLocked(false);
                     }
                 }
             });
             
             mainSplit.setSplitPosition(150, HorizontalSplitPanel.UNITS_PIXELS);
             return b;
         }

		@Override
		public void buttonClick(ClickEvent event) {
			
			Button source =  event.getButton();
			if(receiveClothBtn == source){
				ReceiveClothView receiveClothView = new ReceiveClothView(null);
	             mainSplit.setSecondComponent(receiveClothView);
			}else if(takeClothBtn == source){
				this.showNotification("还没有实现",Window.Notification.TYPE_WARNING_MESSAGE);
			}else if(statisticBtn == source){
				this.showNotification("还没有实现",Window.Notification.TYPE_WARNING_MESSAGE);
			}else if(systemBtn == source){
				 //设置系统设置
	             SystemManagerTabSheet  smt = new SystemManagerTabSheet();
	             mainSplit.setSecondComponent(smt);
			}
				
		}
    }
    
	private class BreadCrumbs extends CustomComponent {
		HorizontalLayout layout;
		BreadCrumbs() {
			layout = new HorizontalLayout();
			layout.setSpacing(true);
			setCompositionRoot(layout);
			setStyleName("breadcrumbs");
		}
	}
}

